import java.util.Scanner;
public class Main{
        public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int arr[]=new int[100];
        int count=0;
        while (true) {
        int x= sc.nextInt();
        int n=sc.nextInt();
        int a=x%1000;

        if (x==0&&n==0) {
        break;
        }
        count++;



        for (int i = 0; i <n-1 ; i++) {
        a=a*x%1000;
        }

        arr[count-1]=a;

        }

        for (int i = 0; i < count; i++)
        {System.out.println(arr[i]);

        }
        }
        }